<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>

<div class="container">

    <h2>商品类别列表</h2>

    <p>
        <input id="txtSearch">
        <button type="button" id="btnTypeSearch">查询</button>
    </p>
    <p>
        <a href="category_add.html">
            <button class="btn btn-info" type="button">添加商品类别</button>
        </a>
    </p>
    <table class="table">
        <thead>
        <tr>
            <th>类别编号</th>
            <th>类别名称</th>
            <th>创建时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        </tbody>
    </table>

</div>

</body>
<script src="js/jquery-3.2.1.js"></script>
<script src="js/layer/layer.js"></script>

<script>

    $(function () {
        getAllType(null);

        $('#btnTypeSearch').click(function () {
            getAllType($('#txtSearch').val());
        })
    })

    function getAllType(name) {
        $('tbody').empty();
        $.post('query_all_type.do', {tname: name}, function (data) {
            if (data.code == 200) {
                $.each(data.result, function (index, obj) {
                    var tr = $('<tr></tr>');
                    var td1 = $('<td></td>').text(obj.tno);
                    var td2 = $('<td></td>').text(obj.tname);
                    var td3 = $('<td></td>').text(obj.createTime);
                    var td4 = $('<td></td>').html('<button type="button" onclick="editType(' + obj.tno + ')" class="btn btn-primary">编辑</button>&nbsp;&nbsp;&nbsp;&nbsp;<button type="button" onclick="delType(' + obj.tno + ')" class="btn btn-danger">删除</button>');
                    tr.append(td1).append(td2).append(td3).append(td4);
                    $('tbody').append(tr);
                })
            }
        }, "JSON");
    }

    function editType(id) {
        location.href = "category_edit.html?tno=" + id;
    }

    function delType(id) {
        //console.log(id)
        layer.confirm('是否确认删除该商品类型', {icon: 3, title: '删除提示'}, function (index) {
            //do something
            $.getJSON('del_type.do', {tno: id}, function (data) {
                if (data.code == 200) {
                    layer.msg(data.msg, {icon: 1, time: 2000}, function () {
                        getAllType(null);
                    })
                }
            })
            layer.close(index);
        });

    }
</script>
</html>